import {makeAutoObservable, reaction} from "mobx";
import Taro from "@tarojs/taro";
let authorizationStorageKey = "Authorization";

export class AuthorizationStore {

  authorization: string|null = null


  constructor() {
    makeAutoObservable(this)
    this.authorization = Taro.getStorageSync(authorizationStorageKey)??null
    reaction(() => this.authorization, (arg) => {
      if (arg) {
        Taro.setStorageSync(authorizationStorageKey, arg)
      } else {
        Taro.removeStorageSync(authorizationStorageKey)
      }
    })
  }

  setAuthorization(authorization: string | null) {
    this.authorization = authorization
  }

  async logOut() {
    this.authorization = null;
  }

  get hasLogIn() {
    return !!this.authorization
  }


}

export default new AuthorizationStore()
